package com.suncaper.wcss.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.wcss.common.PageDTO;
import com.suncaper.wcss.model.dto.ProductDTO;
import com.suncaper.wcss.model.vo.ApplicationVO;
import com.suncaper.wcss.model.vo.ProductVO;
import com.suncaper.wcss.service.ApplicationService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.suncaper.wcss.common.JsonResponse;
import com.suncaper.wcss.service.ProductService;
import com.suncaper.wcss.model.domain.Product;

import java.util.Objects;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2021-11-12
 * @version v1.0
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    private final Logger logger = LoggerFactory.getLogger( ProductController.class );

    @Autowired
    private ProductService productService;
    @Autowired
    private ApplicationService applicationService;

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        productService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateProduct(@PathVariable("id") Long  id,Product  product) throws Exception {
        product.setId(id);
        productService.updateById(product);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Product
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Product  product) throws Exception {
        productService.save(product);
        return JsonResponse.success(null);
    }

    /**
     * 寒衣添加
     */
    @PostMapping("/save")
    @ResponseBody
    public JsonResponse save(ProductDTO product){
        productService.insert(product);
        return JsonResponse.success(true);
    }
    /**
     * 寒衣列表接口
     */
    @GetMapping("/list")
    public String list(PageDTO page, Product product, Model model){
        Page<ProductVO> voPage = productService.pageList(page,product);
        model.addAttribute("page",voPage);
        //如果前端传递了参数，需要将这个条件回显到页面（因为跳转页面会全刷新页面）
        if(Objects.nonNull(product.getBatchId())){
            model.addAttribute("queryBacth",product.getBatchId());
        }
        return "pages/product/product-list";
    }


    /**
     * 根据id查询
     */
    @GetMapping("/getById")
    public String getById(Long id,Model model){
        ProductVO productVO = productService.selectById(id);
        model.addAttribute("product",productVO);
        return "pages/product/product-edit";
    }
    /**
     * 根据id查询(跳转界面不一样)
     */
    @GetMapping("/checkById")
    public String checkById(Long id,Model model){
        ProductVO productVO = productService.selectById(id);
        model.addAttribute("product",productVO);
        ApplicationVO applicationVO = applicationService.getBySnAndBatchId();
        model.addAttribute("app",applicationVO);
        return "pages/product/product-detail";
    }
    /**
     * 修改寒衣
     */
    @PostMapping("/edit")
    @ResponseBody
    public JsonResponse edit(ProductDTO productDTO){
        productService.edit(productDTO);
        return JsonResponse.success(true);
    }
}

